package cn.itcast.service.impl;

import cn.itcast.constant.MessageConstant;
import cn.itcast.dao.OrderDao;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Order;
import cn.itcast.pojo.Orderlisl;
import cn.itcast.service.OrderService;
import cn.itcast.util.IdWorker;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class OrderServiceImpl implements OrderService {


    @Autowired
    private OrderDao orderDao;

    @Autowired
    private IdWorker idWorker ;

    /**
     * 提交预约数据
     *
     * @param order
     * @return
     */
    @Override
    public Result submit(Order order) {
        //判断是否已经预约该套餐
        String idCard = order.getIdCard();
        Integer mealId = order.getSetmealId();
        String name = this.orderDao.findOrder(idCard, mealId);
        //套餐的类型,无重复预约
        if (StringUtils.isNotEmpty(name)) {
            return new Result(false, MessageConstant.HAS_ORDERED);
        }

        //当天可预约人数
        int count = this.orderDao.findCount(order.getOrderDate());
        //判断该日期未填写,无预约人数
        Date date = order.getOrderDate();
        if (date == null && count == 0) {
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        //设置id
        order.setId(idWorker.nextId()+"");
        //还未支付
        order.setOrderStatus(0);
        order.setPayStatus(0);
        //在数据库保存该用户信息
        this.orderDao.save(order);
        //满足以上条件,可预约人数-1,已预约人数+1
        this.orderDao.downNumber(date);

        return new Result(true, MessageConstant.ORDER_SUCCESS, order.getId());
    }

    /**
     * 根据id 查询用户
     *
     * @param id
     * @return
     */
//    @Override
//    public Order findById(String id) {
//        Order order = this.orderDao.findById(id);
//        return order;
//    }

    /**
     * 根据用户id查询
     *
     * @param id
     * @return
     */
    @Override
    public List<Order> findUserAll(Integer id) {
        return this.orderDao.findUserAll(id);
    }
    /**
     * 预约列表中根据id查询单个对象信息的方法
     * @param id
     * @return
     */
    @Override
    public Order findById(String id) {
        if (id==null){
            return null;
        }
        return orderDao.findById(id);
    }
    /**
     * 预约列表中修改的方法
     * @param order
     * @return
     */
    @Override
    public int updateById(Order order) {
        if (order==null){
            return 0;
        }
        return orderDao.updateById(order);
    }
    /**
     * 预约列表中添加的方法
     * @param order
     * @return
     */
    @Override
    public int save(Order order) {
        if (order==null){
            return 0;
        }
        order.setId(UUID.randomUUID().toString());
        order.setOrderStatus(0);
        return orderDao.save(order);
    }

    /**
     *到诊未到诊
     */


    /**
     * 预约列表中根据条件查询分页的方法
     * @return
     */
    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString, String orderType, Date minTime, Date maxTime) {
        if (currentPage==0 && pageSize==0){
            PageHelper.startPage(1,10);
        }
        PageHelper.startPage(currentPage,pageSize);
        Page<Orderlisl> orderPage = orderDao.findByPage(queryString, orderType, minTime, maxTime);

        return new PageResult(orderPage.getTotal(),orderPage.getResult());

    }


    @Override
    public boolean update(String id, Integer arrival) {

        this.orderDao.update(id,arrival);
        return false;
    }
}

